{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "\n",
    " #https://blog.csdn.net/FrankieHello/article/details/98677289\n",
    "        \n",
    "5分钟：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine5m?symbol=rb1910\n",
    "\n",
    "15分钟：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine15m?symbol=rb1910\n",
    "\n",
    "30分钟：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine30m?symbol=rb1910\n",
    "\n",
    "60分钟：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine60m?symbol=rb1910\n",
    "\n",
    "日K线：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesDailyKLine?symbol=rb1910\n",
    "\n",
    "\n",
    "5分钟：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/CffexFuturesService.getCffexFuturesMiniKLine5m?symbol=IF1908\n",
    "\n",
    "15分钟：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/CffexFuturesService.getCffexFuturesMiniKLine15m?symbol=IF1908\n",
    "\n",
    "30分钟：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/CffexFuturesService.getCffexFuturesMiniKLine30m?symbol=IF1908\n",
    "\n",
    "\n",
    "60分钟：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/CffexFuturesService.getCffexFuturesMiniKLine60m?symbol=IF1908\n",
    "\n",
    "日线：\n",
    "http://stock2.finance.sina.com.cn/futures/api/json.php/CffexFuturesService.getCffexFuturesDailyKLine?symbol=IF1908\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from urllib import request\n",
    "import json\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "def get_data(id, filename='data.csv'):\n",
    "    url = 'http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine60m?symbol='\n",
    "    # url = 'http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine5m?symbol='\n",
    "    url = url + id\n",
    "    print(url)\n",
    "    req = request.Request(url)\n",
    "    rsp = request.urlopen(req)\n",
    "    res = rsp.read()\n",
    "    res_json = json.loads(res)\n",
    "\n",
    "\n",
    "    bar_list = []\n",
    "\n",
    "    res_json.reverse()\n",
    "    for line in res_json:\n",
    "        bar = {}\n",
    "        bar['datetime'] = line[0]\n",
    "        bar['open'] = float(line[1])\n",
    "        bar['high'] = float(line[2])\n",
    "        bar['low'] = float(line[3])\n",
    "        bar['close'] = float(line[4])\n",
    "        bar['volume'] = int(line[5])\n",
    "        bar_list.append(bar)\n",
    "\n",
    "    df = pd.DataFrame(data=bar_list)\n",
    "    print(df.head())\n",
    "    df.to_csv('F:\\\\HQData\\\\future\\\\'+filename+'.csv', index=None)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine60m?symbol=eg2201\n",
      "              datetime    open    high     low   close  volume\n",
      "0  2021-09-09 14:00:00  5225.0  5243.0  5221.0  5242.0   20675\n",
      "1  2021-09-09 15:00:00  5244.0  5270.0  5218.0  5239.0   47202\n",
      "2  2021-09-10 10:00:00  5331.0  5344.0  5276.0  5276.0  102023\n",
      "3  2021-09-10 11:00:00  5286.0  5322.0  5248.0  5248.0   42930\n",
      "4  2021-09-10 11:30:00  5244.0  5255.0  5220.0  5248.0   44777\n",
      "http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine60m?symbol=rb2201\n",
      "              datetime    open    high     low   close  volume\n",
      "0  2021-10-08 22:00:00  5765.0  5818.0  5740.0  5818.0  397922\n",
      "1  2021-10-08 23:00:00  5814.0  5868.0  5810.0  5868.0  331006\n",
      "2  2021-10-09 00:00:00  5866.0  5866.0  5866.0  5866.0     774\n",
      "3  2021-10-11 10:00:00  5828.0  5859.0  5761.0  5761.0  631411\n",
      "4  2021-10-11 11:00:00  5761.0  5812.0  5754.0  5812.0  238551\n"
     ]
    }
   ],
   "source": [
    "# get_data('eg2201', 'eg2201')\n",
    "get_data('rb2201', 'rb2201')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
